/**
 * Created by chengcong on 2017/11/20.
 */
// const sokceturl = "http://47.90.123.205:8000";
$(function(){
    var msg=window.sessionStorage['user_msg'];
    msg=JSON.parse(msg);
    socket.emit('login',{
        username :msg.username,
        openid  :null,
        sex : 0,
        marking :1,//1pc  2wx  3app  Number
        userrole :msg.userrole,//登陆角色 (医生(1)，用户(2)，助理(3)，药师(4),匿名用户(5))
        token:msg.token
    });
	var msg = window.sessionStorage['user_msg'];
	msg = JSON.parse(msg);
	console.log(msg);
    socket.on('private_msg', function(data) {
        console.log(data);
        window.top.document.getElementsByClassName('unfinish2')[0].innerHTML =
            parseFloat(window.top.document.getElementsByClassName('unfinish2')[0].innerHTML) + 1;
        window.top.document.getElementsByClassName('unfinish2')[0].style.display = 'inline-block';
    })
	$.ajax({
		url: url + '/server/allCertificate',
		type: 'post',
		data: {
			limit:10,
			page:1
		},
		headers: {
			'Authorization': 'Bearer ' + msg.token
		},
		success: function(data) {
			console.log(data);
			if(data.code == 200) {
				window.top.document.getElementsByClassName('unfinish2')[0].innerHTML = data.count;
				// $('.unfinish').text(data.data.length)
				if((data.OTC.length + data.Rx_Drug.length) > 0) {
					window.top.document.getElementsByClassName('unfinish2')[0].style.display = 'inline-block';
				} else {
					window.top.document.getElementsByClassName('unfinish2')[0].style.display = 'none';
				}
				$('table tbody').html('');
				if(data.OTC.length > 0 || data.Rx_Drug.length > 0) {
					if(data.OTC.length > 0) {
						for(var i = 0; i < data.OTC.length; i++) {
							$('table tbody').append('<tr><td userid="'+data.OTC[i].userids+'"><input type="checkbox" value="' + data.OTC[i]._id + '"></td><td>' + 10086 + '</td><td>' + data.OTC[i].doctorName + '</td>' +
								'<td>非处方未处理</td>' +
								'<td><button class="btn btn-danger detailbtn" listid="' + data.OTC[i]._id + '" status="2">立即处理</button></td></tr>');
						}
						$('.detailbtn').on('click', function(e) {
							console.log(1111);
							self.location = '../../postpaper/postpaper.html?detailId=' + $(e.target).attr('listid') + '&&type=' + $(e.target).attr('status');
						});

					}
					if(data.Rx_Drug.length > 0) {
						for(var i = 0; i < data.Rx_Drug.length; i++) {
							$('table tbody').append('<tr style="background:pink;"><td userid="'+data.Rx_Drug[i].userids+'"><input type="checkbox" value="' + data.Rx_Drug[i]._id + '"></td><td>' + 10086 + '</td><td>' + data.Rx_Drug[i].doctorName + '</td>' +
								'<td>处方未处理</td>' +
								'<td><button class="btn btn-danger detailbtn" listid="' + data.Rx_Drug[i]._id + '" status="2">立即处理</button></td></tr>');
						}
						$('.detailbtn').on('click', function(e) {
							console.log(1111);
							self.location = '../../postpaper/postpaper.html?detailId=' + $(e.target).attr('listid') + '&&type=' + $(e.target).attr('status');
						});
					}

				} else {
					$('table tbody').append('<tr><td colspan="5"><img src="../../img/empty-b.png" alt="" style="margin-top: 200px"></td></tr>');
				}
			} else {
				alert(data.mes)
			}
			//药师审核
			$('.checkbtn').on('click', function() {
				var input = $('table tbody tr input');
				var idlist = [];
				var useridlist=[];
				var cancheck=true;
				for(var i = 0; i < input.length; i++) {
					if(input[i].checked == true) {
						idlist.push($(input[i]).val());
						useridlist.push($(input[i]).parent().attr('userid'));
						if($(input[i]).parent().next().next().next().text()=='处方未处理'){
							cancheck=false;
						}
					}
				}
				if(cancheck==false){
					alert('选项包含处方药的诊断单，请重新选择')
				}else{
                    if(idlist.length<1){
                        alert('请选择需要处理的事项')
                    }else{
                        $.ajax({
                            url: url + '/server/pharmacistManyCheck',
                            type: 'post',
                            data: {
                                _idArr : JSON.stringify(idlist)
                            },
                            headers: {
                                'Authorization': 'Bearer ' + msg.token
                            },
                            success: function(data) {
                                console.log(data);
                                if(data.code == 200) {
                                    alert('审核通过');
                                    $.ajax({
                                        url: url + '/server/allCertificate',
                                        type: 'post',
                                        data: {

                                        },
                                        headers: {
                                            'Authorization': 'Bearer ' + msg.token
                                        },
                                        success: function(data) {
                                            console.log(data);
                                            if(data.code == 200) {
                                                window.top.document.getElementsByClassName('unfinish2')[0].innerHTML = data.OTC.length + data.Rx_Drug.length;
                                                // $('.unfinish').text(data.data.length)
                                                if((data.OTC.length + data.Rx_Drug.length) > 0) {
                                                    window.top.document.getElementsByClassName('unfinish2')[0].style.display = 'inline-block';
                                                } else {
                                                    window.top.document.getElementsByClassName('unfinish2')[0].style.display = 'none';
                                                }
                                            }
                                        }
                                    });
                                    //socket.通知信息
                                     socket.emit('private_data',{
                                     	dataType:"notification",
                                     	userid:useridlist,
                                     	msg:'问诊单审核通过，请查看',
                                     	_id: idlist
                                     })
                                    $.ajax({
                                        type: "post",
                                        dataType: "json",
                                        url: "https://xcx.juseyx.cn/service/producer",
                                        data: {
                                            topic: "juseservice",
                                            msg: JSON.stringify({
                                                userid:useridlist,
                                                msg: '问诊单审核通过，请查看',
                                                _id: idlist
                                            }),
                                            msgkey: "key"
                                        },
                                        async: true,
                                        success: function(res) {
                                            console.log(res);
                                            if(res.code == 200) {
                                                if(res.defeat>0){
                                                    alert('有'+res.defeat+'条已被处理')
                                                }
                                                self.location = 'unfinish.html';
                                            } else {
                                                alert('操作有误');
                                            }
                                        },

                                        error: function(err) {
                                            console.log(err)
                                        }
                                    });
                                }

                            }
                        });
                    }
				}

			});

		}
	});
})